package com.eqxiu.ieditor.weixin.event.impl;

import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.eqxiu.ieditor.weixin.dto.BaseEvent;
import com.eqxiu.ieditor.weixin.event.AbstractEventHandler;

@Component
@Qualifier("subscribeHandler")
public class SubscribeHandler extends AbstractEventHandler {
	@Autowired
	private StringRedisTemplate redisTemplate;

	@Override
	public boolean canHandle(String eventType) {
		return BaseEvent.EVENT_TYPE_SUBSCRIBE.equals(eventType);
	}

	@Override
	public String doHandle(Map<String, String> root) {
		String eventKey = root.get("EventKey");
		if (Objects.nonNull(eventKey)) {// 有参数
			String key = eventKey;
			if (key.startsWith("qrscene_")) {
				key = key.replace("qrscene_", "");
				if (key.startsWith("IE:QR:LOGIN_KEY")) {
					redisTemplate.opsForValue().set(key, getFromUser(root));
				}
			}
		}

		return "";
	}

}
